package com.kun.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.index.entity.History;
import com.kun.index.mapper.HistoryMapper;
import com.kun.index.service.HistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 历史记录表 服务实现类
 * </p>
 *
 * @author kunkun
 * @since 2024-03-26
 */
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {

    @Autowired
    private HistoryMapper historyMapper;


    @Override
    public History selectByHistory(History history) {
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",history.getUserId());
        wrapper.eq("blogId",history.getBlogId());
        wrapper.eq("type",history.getType());
        return historyMapper.selectOne(wrapper);
    }

    @Override
    public Integer updateHistory(History history) {
        History history1 = this.selectByHistory(history);
        history.setType(history1.getType());
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",history.getUserId());
        wrapper.eq("blogId",history.getBlogId());
        wrapper.eq("type",history.getType());
        return historyMapper.update(history,wrapper);
    }

    @Override
    public Integer addHistory(History history) {
        return historyMapper.insert(history);
    }

    @Override
    public int deleteHistory(History history) {
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",history.getUserId());
        wrapper.eq("blogId",history.getBlogId());
        wrapper.eq("type",history.getType());
        return historyMapper.delete(wrapper);
    }

    @Override
    public Map<String ,Object> queryHistoryByConditions(HashMap<String, Object> map) {
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        HashMap<String, Object> resultMap = new HashMap<>();


        //根据userId和type进行查询
        if (map.get("userId")!=null){
            wrapper.eq("userId",(Integer)map.get("userId"));
        }
        if(map.get("historyType")!=null){
            wrapper.eq("type",(String)map.get("historyType"));
        }
        //根据userId和type进行查询
        if (map.get("blogId")!=null){
            wrapper.eq("blogId",(Integer)map.get("blogId"));
        }
        //根据更新时间降序
        if (map.get("updateTime_desc")!=null){
            wrapper.orderByDesc("updateTime");
        }
        //分页查询
        if (map.get("pageIndex")!=null && map.get("pageSize")!=null){
            Page<History> page = new Page((Integer) map.get("pageIndex"),(Integer) map.get("pageSize"));
            Page<History> selectPage = historyMapper.selectPage(page, wrapper);
            List<History> list = selectPage.getRecords();
            long size = selectPage.getTotal();
            resultMap.put("histories",list);
            resultMap.put("size",size);
        }else{
            List<History> histories = historyMapper.selectList(wrapper);
            resultMap.put("histories",histories);
            resultMap.put("size",histories.size());
        }
        return resultMap;
    }
}
